package com.wjk.StuManager.Controller;

import com.wjk.StuManager.Domain.Student;
import com.wjk.StuManager.Service.StuService;

import java.util.Scanner;

public abstract class BaseStuController {
    Scanner sc=new Scanner(System.in);
    StuService stuService=new StuService();
    public final void start() {
        stuloop:
        while (true){
            System.out.println("------欢迎来到学生管理系统------");
            System.out.println("请输入你要执行的操作：1.添加学生 2.删除学生 3.修改学生 4.查看所有学生 5.退出");
            String choice=sc.next();
            switch (choice){
                case "1":
                    addStu();
                    break;
                case "2":
                    delStu();
                    break;
                case "3":
                    updateStu();
                    break;
                case "4":
                    findAllStu();
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    break stuloop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;

            }
        }
    }

    private final void findAllStu() {
        Student[] stus=stuService.findAllStu();
        if (stus==null){
            System.out.println("暂无信息，请添加后查看");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu=stus[i];
            if (stu!=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBirthday());
            }
        }
    }

    private final void updateStu() {
        String updateId=inputId();
        Student stu=inputStuInfo(updateId);
        stuService.updateStu(updateId,stu);
        System.out.println("修改成功！");
    }

    private final void delStu() {
        String delId=inputId();
        stuService.delStuById(delId);
        System.out.println("删除成功！");
    }

    public final void addStu() {
        String id;
        while (true){
            System.out.println("请输入学号：");
            id=sc.next();
            boolean result=stuService.isExists(id);
            if (result){
                System.out.println("您输入的学号已经存在，请重新输入");
            }else {
                break;
            }
        }
        Student stu=inputStuInfo(id);
        boolean result=stuService.addStu(stu);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    public final String inputId(){
        String id;
        while (true){
             System.out.println("请输入学号：");
             id=sc.next();
             boolean result=stuService.isExists(id);
             if (!result){
                 System.out.println("您输入的学号不存在，请重新输入");
             }else {
                 break;
             }
        }
        return id;
    }

    public abstract Student inputStuInfo(String id);
}
